﻿using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.JSON;
using Kingdee.BOS.Util;
using System;
using System.ComponentModel;

namespace Jac.XkDemo.BOS.Business.PlugIn
{
    /// <summary>    /// 【表单插件】表格动态创建列    /// </summary>    
    [Description("【表单插件】表格动态创建列"), HotUpdate]
    public class CreateDynamicListFormPlugIn : AbstractDynamicFormPlugIn
    {
        #region var 
        
        /// <summary>        
        /// 单据体标识        
        /// </summary>        
        private const string entityKey = "F_Jac_Entity";

        /// <summary>        
        /// 需要动态创建的列的列名        
        /// </summary>        
        private string[] fieldNames = { "编码", "名称", "备注" };
        #endregion

        #region event  

        /// <summary>        
        /// 动态创建表格列并绑定数据        
        /// </summary>        
        /// <param name="e"></param>        
        public override void AfterBindData(EventArgs e)
        {
            base.AfterBindData(e);
            BindData();
        }

        /// <summary>        
        /// 主菜单点击事件       
        /// </summary>        
        /// <param name="e"></param>        
        public override void BarItemClick(BarItemClickEventArgs e)
        {
            base.BarItemClick(e);
            if (e.BarItemKey.EqualsIgnoreCase("tbRefresh"))
            {
                BindData();
            }
        }
        /// <summary>        
        /// 动态创建表格列并绑定数据       
        /// </summary>       
        private void BindData()
        {
            var columnNames = this.View.Model.GetValue("F_Jac_Text") as string;
            if (!string.IsNullOrWhiteSpace(columnNames))
            {
                fieldNames = columnNames.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            }
            else
            {
                fieldNames = new[] { "编码", "名称", "备注" };
            }
            var actionData = new JSONObject();
            actionData["key"] = entityKey;
            actionData["methodname"] = "CreateDyanmicList";
            JSONArray coMeta = new JSONArray();
            //添加需要创建的列           
            coMeta.Add(GetColumns());
            coMeta.Add("");
            actionData["args"] = coMeta;
            this.View.AddAction("InvokeControlMethod", actionData, x =>
            {
                //单据体动态创建列之后通过回调方式为该单据体添加数据               
                var grid = this.View.GetControl<EntryGrid>(entityKey);
                grid.SetData(GetEntityData());
            });
        }

        /// <summary>        
        /// 生成动态列指令集        
        /// </summary>        
        /// <returns></returns>       
        private JSONObject GetColumns()
        {
            // 生成构建表格的指令集           
            var entityAppearance = this.View.LayoutInfo.GetEntityAppearance(entityKey);
            var gridMeta = entityAppearance.CreateControl();
            gridMeta.Put("primaryKey", ExtConst.GRID_INDEX_FIELD_KEY);
            gridMeta.Put("indexKey", ExtConst.GRID_INDEX_FIELD_KEY);
            gridMeta.Put("startRow", 0);
            gridMeta.Put("pageSize", entityAppearance.PageRows);
            //gridMeta.Put("editable", false);            
            // 生成列           
            var columns = (JSONArray)gridMeta["columns"]; for (var i = 0; i < fieldNames.Length; ++i)
            {
                var fieldName = fieldNames[i]; var col = new JSONObject(); 
                var xtype = "textfield"; 
                col.Put("xtype", xtype); 
                col.Put("colIndex", i + 2); 
                col.Put("dataIndex", fieldName); 
                col.Put("header", fieldName); 
                col.Put("width", "300"); 
                col.Put("visible", true);
                // 如果需要某列的单元格可编辑，必须设置该列的编辑控件属性                
                // col.Put("editor", CreateEditorControl(xtype));               
                columns.Add(col);
            }
            return gridMeta;
        }

        /// <summary>        
        /// 把元素对应的编辑控件输出为Json对象        
        /// </summary>       
        /// <returns></returns>        
        private JSONObject CreateEditorControl(string xtype)
        {
            var ctlRet = new JSONObject();
            ctlRet.Put("xtype", xtype);
            ctlRet.Put("editable", true);
            return ctlRet;
        }

        /// <summary>       
        /// 生成表格数据               
        /// </summary>        
        /// <returns></returns>        
        private JSONObject GetEntityData()
        {
            var data = new JSONObject(); var rows = new JSONArray();
            for (var x = 1; x <= 20; ++x)
            {
                var row = new JSONArray(); 
                row.Add(x); 
                row.Add(x);
                foreach (var fieldName in fieldNames)
                {
                    row.Add(string.Format("{0}：{1}", fieldName, Guid.NewGuid()));
                }
                rows.Add(row);
            }
            data.Put("rows", rows);
            return data;
        }
        #endregion
    }
}


